{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!

This is a template for creating compiled Enum and Flag pyx information

}}
{{#program:enums}}

{{#enum:flags?}}
cdef __EnumFlagsData __{{enum:name}}_enum_data  = __EnumFlagsData._fbthrift_create(thrift.py3.types.createEnumFlagsData[c{{enum:name}}](), {{enum:name}})
{{/enum:flags?}}{{^enum:flags?}}
cdef __EnumData __{{enum:name}}_enum_data  = __EnumData._fbthrift_create(thrift.py3.types.createEnumData[c{{enum:name}}](), {{enum:name}})
{{/enum:flags?}}


@__cython.internal
@__cython.auto_pickle(False)
cdef class __{{enum:name}}Meta(thrift.py3.types.EnumMeta):
    def _fbthrift_get_by_value(cls, int value):
        return __{{enum:name}}_enum_data.get_by_value(value)

    def _fbthrift_get_all_names(cls):
        return __{{enum:name}}_enum_data.get_all_names()

    def __len__(cls):
        return __{{enum:name}}_enum_data.size()

    def __getattribute__(cls, str name not None):
        if name.startswith("__") or name.startswith("_fbthrift_") or name == "mro":
            return super().__getattribute__(name)
        return __{{enum:name}}_enum_data.get_by_name(name)


@__cython.final
@__cython.auto_pickle(False)
cdef class {{enum:name}}(thrift.py3.types.{{^enum:flags?}}CompiledEnum{{/enum:flags?}}{{#enum:flags?}}Flag{{/enum:flags?}}):
    cdef get_by_name(self, str name):
        return __{{enum:name}}_enum_data.get_by_name(name)

    {{#enum:flags?}}

    def __invert__(self):
        return __{{enum:name}}_enum_data.get_invert(self.value)
    {{/enum:flags?}}

    @staticmethod
    def __get_metadata__():
        cdef __fbthrift_cThriftMetadata meta
        EnumMetadata[c{{enum:name}}].gen(meta)
        return __MetadataBox.box(cmove(meta))

    @staticmethod
    def __get_thrift_name__():
        return "{{program:name}}.{{enum:name}}"

    def _to_python(self):
        import importlib
        {{!TODO: accommodate root_module_prefix}}
        python_types = importlib.import_module(
            "{{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_types"
        )
        return python_types.{{enum:name}}(self.value)

    def _to_py3(self):
        return self

    def _to_py_deprecated(self):
        return self.value


__SetMetaClass(<PyTypeObject*> {{enum:name}}, <PyTypeObject*> __{{enum:name}}Meta)

{{/program:enums}}
